package com.learn.learnredis.jedis.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;

/**
 * @author wangxing
 * @version 2020/7/21 7:14 Administrator
 */
public class SocketNIO {
    public static void main(String[] args) {
        LinkedList<SocketChannel> clients = new LinkedList<>();
        try {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.bind(new InetSocketAddress(80));
            serverSocketChannel.configureBlocking(false);//只让接受客户端连接,不进行阻塞
            while(true){
                //接受客户端的连接
                SocketChannel client = serverSocketChannel.accept();//不会阻塞
                if(client == null){
                    System.out.println("null===============");
                }else{
                    client.configureBlocking(false);//
                    int port = client.socket().getPort();
                    System.out.println("client port :"+port);
                    clients.add(client);
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
